/**
* 2110101 Computer Programming
* Lab #2 : Arithmetic Expression
* @author somchaip@chula.ac.th
* @version 1.00 27/06/2002
*/
import jlab.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Lab2 extends Applet {
/**
* This method computes (px, py) which is the intersection point
* obtained by drawing a line from (cx, cy), the center of a circle
* whose radius is cr, to (mx, my), a mouse pointer position.
* @param cx, cy - circle center's coordinate
* @param cr - circle's radius
* @param mx, my - mouse pointer's coordinate
* @return <code>Point</code> of the intersection
*/
public static Point compute(int cx, int cy, int cr, int mx, int my) {
int px, py;
//---- begin your code here ----
double d;
d = Math .sqrt ((my-cy)*(my-cy)+(mx-cx)*(mx-cx));
py = (int)(((my - cy) / d) * cr) + cy;
px = (int)(((mx - cx) / d) * cr) + cx;
//---- end your code here ------
return new Point(px, py);
} // end of compute
//--------------------------------------------------------------------
private Point mouse;
private Point leftEye, rightEye, leftPupil, rightPupil;
private int curWidth = 0, curHeight = 0;
private Image imageBuffer;
private Graphics gBuffer;
private final int EYE_RADIUS = 20, PUPIL_RADIUS = 5;
/**Initialize the applet*/
public void init() {
this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
this_mouseMoved(e);
}
}
);
}
private void this_mouseMoved(MouseEvent e) {
mouse = e.getPoint();
repaint();
}
/**Start the applet*/
public void start() {
mouse = new Point(getSize().width / 2, 10000);
setEyePos(new Point (getSize().width / 2, getSize().height / 2));
repaint();
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g) {
int newWidth = getSize().width;
int newHeight = getSize().height;
// implement double buffering to reduce screen flicker
if (imageBuffer == null ||
newWidth != curWidth || newHeight != curHeight) {
imageBuffer = createImage(newWidth, newHeight);
System.out.println("w = " + newWidth + ", h = " + newHeight);
if (gBuffer != null) gBuffer.dispose();
gBuffer = imageBuffer.getGraphics();
curWidth = newWidth;
curHeight = newHeight;
setEyePos(new Point(curWidth / 2, curHeight / 2));
}
gBuffer.setColor(Color.pink);
gBuffer.fillRect(0, 0, newWidth, newHeight);
drawEye(gBuffer);
g.drawImage(imageBuffer, 0, 0, this);
}
private void setEyePos(Point center) {
leftEye = new Point(center.x - EYE_RADIUS - 3, center.y);
rightEye = new Point(center.x + EYE_RADIUS + 3, center.y);
}
private void drawEye(Graphics g) {
// Draw the white eyes
g.setColor(Color.white);
fillCircle(g, leftEye, EYE_RADIUS);
fillCircle(g, rightEye, EYE_RADIUS);
g.setColor(Color.black);
g.drawOval(leftEye.x - EYE_RADIUS, leftEye.y - EYE_RADIUS,
2 * EYE_RADIUS, 2 * EYE_RADIUS);
g.drawOval(rightEye.x - EYE_RADIUS, rightEye.y - EYE_RADIUS,
2 * EYE_RADIUS, 2 * EYE_RADIUS);
// Draw the pupils
g.setColor(Color.black);
leftPupil = computePupilPos(leftEye);
fillCircle(g, leftPupil, PUPIL_RADIUS);
rightPupil = computePupilPos(rightEye);
fillCircle(g, rightPupil, PUPIL_RADIUS);
}
private Point computePupilPos(Point eye) {
return compute(eye.x, eye.y, EYE_RADIUS - PUPIL_RADIUS,
mouse.x, mouse.y);
}
private void fillCircle(Graphics g, Point center, int radius) {
g.fillOval(center.x - radius, center.y - radius,
2 * radius, 2 * radius);
}
//--------------- Main method ---------------------
public static void main(String[] args) {
Lab2 applet = new Lab2();
String title = "JLab : JEyes";
// set up window frame
Frame frame = new Frame(title);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
frame.add(applet, BorderLayout.CENTER);
frame.setSize(300, 300);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
applet.init();
applet.start();
frame.setVisible(true);
try {
JLabIO.setWindowAlwaysOnTop(title);
} catch (Error e) {}
}
}
//-------------------------------------------------------------
|